package getter

import (
	"server/game/cache"
	"server/game/model"

	"github.com/hwcer/cosmo"
	"github.com/hwcer/updater"
	"github.com/hwcer/yyds/context"
)

const itemsPageCursorName = "items_page_cursor"

//func init() {
//	Register(GetItemsWithPage, "items")
//}

type itemsPageCursor []*model.Items

func getOrCreateItemsPageCursor(u *updater.Updater) any {
	var r itemsPageCursor
	handle := cache.GetItems(u)
	handle.Range(func(s string, v *model.Items) bool {
		r = append(r, v)
		return true
	})
	return r
}

/**
 * @name GetItemsWithPage
 * @param int bag 背包ID
 * 道具列表,标准分页
 */
func GetItemsWithPage(c *context.Context) (r any) {
	paging := &cosmo.Paging{}
	if err := c.Bind(paging); err != nil {
		return err
	}
	paging.Init(300)
	paging.Rows = []*model.Items{}
	tx := db.Where("uid = ?", c.Uid()).Page(paging)
	if tx.Error != nil {
		return tx.Error
	}
	return paging
	//
	//i := c.Player.Updater.Process.GetOrCreate(c.Player.Updater, itemsPageCursorName, getOrCreateItemsPageCursor)
	//cursor := i.(itemsPageCursor)
	//paging.Result(len(cursor))
	//
	//defer func() {
	//	if paging.Size >= paging.Total {
	//		c.Player.Updater.Process.Delete(itemsPageCursorName)
	//	}
	//	//r = paging.Rows
	//}()
	//if len(cursor) == 0 {
	//	return paging
	//}
	//offset := paging.Offset()
	//if offset >= paging.Record {
	//	return paging
	//}
	//limit := offset + paging.Size
	//if limit > paging.Record {
	//	limit = paging.Record
	//}
	//paging.Rows = cursor[offset:limit]
	//return paging
}
